package com.wangxi.boot11demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        String dirName = "D:/files";
        File dirPath = new File(dirName);
        if(!dirPath.exists()){
            dirPath.mkdirs();
        }
        picFile.transferTo(new File(dirName+"/"+fileName));
        return fileName;
    }

    @RequestMapping("remove")
    public void remove(String fileName){
        String name = "D:/files/"+fileName;
        new File(name).delete();
    }
}
